/**
 * 乘客行程订单流转操作
 *
 * 已经废弃
 *
 */
define(["app", "comm/pager/PagerControl", "js/services/MessageBox", "js/services/MuseService"], function (app) {

    var deps = ["$scope", "$state", "$modal", "MessageBox", "MuseService", '$modalInstance', 'trip'];

    function controller ($scope, $state, $modal, MessageBox, MuseService, $modalInstance, trip) {
        $scope.trip = trip;
        $scope.orginDriver = trip.driver;

        //初始化查询对象 和 分页对象
        app.regsiterCacheData($scope, $state);
        $scope.queryObj = $scope.queryObj || {};
        $scope.pagerObj = $scope.pagerObj || {};

        //获取列表
        $scope.getObjList = function (pageNo, pageSize) {
            var curPage = pageNo || $scope.pagerObj.currentPage || 1;
            var param = {
                page: curPage, pageSize: pageSize || $scope.pagerObj.pageSize,
                param: {
                    name: 'like ' + ($scope.queryObj.name ? $scope.queryObj.name : "")
                }
            };

            MuseService.postObj(baseServices_restfulUrl + "driver/search.json", param).success(
                function (data, status, headers, config) {
                    // console.log(data);
                    if (data.state == 0) {
                        var page = data.data;
                        $scope.driverList = page.data;

                        //分页器
                        $scope.pagerObj = PageCtrl().page($scope.pagerObj, page.total, curPage);
                    }
                })
            .error(function () {
                MessageBox.error('服务器正在开小差zZZ,获取不到数据');
            });
        };

        $scope.getObjList();

        //选择
        $scope.select = function (driver) {
            $scope.selectedDriver = driver;
        };

        $scope.pageChanged = function () {
            PageCtrl().check($scope.pagerObj);
            $scope.getObjList($scope.pagerObj.currentPage, $scope.pagerObj.pageSize);
        }

        $scope.close = function () {
            $modalInstance.dismiss();
        };

        /**
         * 确认是否流转
         * @param targetDriver
         */
        $scope.confirmFlow = function () {

            if (!$scope.reason) {
                MessageBox.error("你还没填写流转理由");
                return;
            }

            if (!$scope.selectedDriver) {
                MessageBox.error("你还没选择要流转的司机");
                return;
            }

            var dialog = MessageBox.confirm("确定流转给这位司机? <span class='text-danger'>" + $scope.selectedDriver.nickname + "</span>");
            dialog.result.then(function () {
                var param = {
                    tripId: $scope.trip.id,
                    targetDriverId: $scope.selectedDriver.id,
                    reason: $scope.reason
                }
                MuseService.post(baseServices_restfulUrl + 'trip/tripFlow.json', param).success(
                    function (data) {
                        if (data.state == 0) {
                            MessageBox.notify({msg: '流转成功！', displayTime: 1000});
                            $modalInstance.close("tripFlowSuccess");
                        } else {
                            MessageBox.error('流转发生错误：' + data.message || "服务器正在开小差");
                        }
                    })
                .error(function () {
                    MessageBox.error('流转错误：服务器正在开小差');
                });
            });
        }

    }

    controller.$inject = deps;
    app.lazy.controller("DriverSelectController", controller);
});
